package com.jlz.actions.console.division;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Standard;
import org.springframework.beans.factory.annotation.Autowired;

import com.jlz.beans.def.DivisionBean;
import com.jlz.managers.def.Divisions;
import com.julewa.Client;
import com.julewa.LOG;

//用更自然的方式使用rest
@Standard(rest="division.ID")
public class IndexAction {
	private @Autowired Divisions divisions;
	private @Autowired DivisionBean division;
	private DivisionBean newdivision = null;
	private @Autowired Client client;
	private static LOG log = new LOG(IndexAction.class);;
	
	public String execute(){
		if(division.getID() == null){
			division = divisions.getRootDivision();
		} else {
			division = divisions.getDivision(division.getID());
		}
		return "division";
	}
	
	@Action("remove")
	public String remove(){
		long divisionId = division.getID();
		DivisionBean beDel = divisions.getDivision(divisionId);
		if(beDel==null)
			client.putError("所删除的division不存在！Id为"+divisionId);
		if(!divisions.remove(beDel))
			client.putError("删除division失败！Id为"+divisionId);
		
		log.info(division.getLabel()+"被删除！");
		return "division";
	}
	
	public void setDivision(DivisionBean division) {
		this.division = division;
	}

	public DivisionBean getDivision() {
		return division;
	}

	public DivisionBean getNewDivision() {
		return newdivision;
	}

}
